package com.lindroy.androidplayer.base

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.paging.CombinedLoadStates
import com.lindroy.androidplayer.viewmodels.PageViewModel
import kotlinx.coroutines.flow.Flow
import java.lang.reflect.ParameterizedType

/**
 * @author Lin
 * @date 2022/1/23
 * @function
 */
abstract class BaseListFragment<VB : ViewDataBinding, VM : PageViewModel> : BaseLazyFragment<VB>() {

    protected val mViewMode: VM by lazy { ViewModelProvider(this)[getVmClazz()] }

    @Suppress("UNCHECKED_CAST")
    //Todo("这里对反射的使用不是很懂，尤其是数组actualTypeArguments下标为何为1？")
    private fun <VM> getVmClazz(): VM =
        (this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as VM


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewMode.refreshLiveData.observe(viewLifecycleOwner) {
            onRefresh()
        }
    }

    /**
     * 下拉刷新
     */
    abstract fun onRefresh()

    /**
     * 点击重连
     */
    abstract fun onRetry(view: View)

    override fun lazyLoad() {}

    override fun onRetryClick(view: View) {
        super.onRetryClick(view)
        onRetry(view)
    }

}